package com.bignerdranch.android.expresshelper.Adapter;

import android.content.Context;
import android.print.PrintAttributes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bignerdranch.android.expresshelper.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/6/29.
 */
public class ResultAdapter extends BaseAdapter {
    private Context mContext;
    private JSONArray mArray;
    private LayoutInflater mInflater;


    public ResultAdapter(JSONArray array, Context context) {
        mArray = array;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mArray.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return mArray.getJSONObject(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.result_item, null);
            holder.position = (TextView) convertView.findViewById(R.id.position);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.point = (ImageView) convertView.findViewById(R.id.point);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        try {
            JSONObject object = mArray.getJSONObject(position);
            holder.position.setText(object.getString("context"));
            holder.time.setText(object.getString("time"));
            if (position==0){
                holder.point.setImageResource(R.drawable.place_now);
                holder.position.setTextColor(mContext.getResources().getColor(R.color.red));
                holder.time.setTextColor(mContext.getResources().getColor(R.color.red));
            }else {
                holder.point.setImageResource(R.drawable.place_past);
                holder.position.setTextColor(mContext.getResources().getColor(R.color.gray));
                holder.time.setTextColor(mContext.getResources().getColor(R.color.gray));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder {
        private TextView position;
        private TextView time;
        private ImageView point;

    }
}
